import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    breadcrumbComp: [], // 组件的面包屑导航
    isReturn: false, // 全局触发后退
  },
  mutations: {
    // 向组件的面包屑导航最后添加一个元素
    SET_BREADCRUMBCOMPADD: (state, val) => {
      state.breadcrumbComp.push(val);
    },
    // 向组件的面包屑导航中去掉最后一个元素
    SET_BREADCRUMBCOMPDELETE: (state) => {
      state.breadcrumbComp.pop();
    },
    // 全局触发后退 开关
    SET_ISRETURN: (state) => {
      state.isReturn = true;
      setTimeout(() => {
        state.isReturn = false;
      }, 100);
    },

  },
  getters: {},
  actions: {
    //取 组件的面包屑导航的最后一个值
    getLastBreadcrumbName({ commit, state }) {
      return new Promise((resolve, reject) => {
        let lastBreadcrumbName = "";
        // 当面包屑导航数组中存在值时，取最后一个值
        if (state.breadcrumbComp.length > 0) {
          lastBreadcrumbName =
            state.breadcrumbComp[state.breadcrumbComp.length - 1];
        }
        resolve(lastBreadcrumbName);
      });
    },
  }
})
